﻿#include "tcpcclientformvb.h"

#include <QDataStream>
#include"Common/pch.h"
tcpcClientForMVB::tcpcClientForMVB()
{

}

void tcpcClientForMVB::dataReceived()
{
    QDataStream in(&socket);
    int typeMsg;
    in>>typeMsg;


    if(typeMsg == MsgType::MVB_READ_REPLY)
    {
        m_readData= socket.readAll();
        emit TransReadMVBData(m_readData);
    }
    else if(typeMsg==MsgType::SERVER_REPLY)
    {
        QString strMsg;
        in>>strMsg;
        emit TcpMsg("返回消息："+strMsg);
    }
}

void tcpcClientForMVB::snedReadMVBDataCommand()
{
    QByteArray data;
    QDataStream in(&data, QIODevice::WriteOnly);
    in.setVersion(QDataStream::Qt_5_12);
    in<<MsgType::MVB_READ;
    socket.write(data);
}

void tcpcClientForMVB::setMaster(bool bMaster)
{
    QByteArray data;
    QDataStream in(&data, QIODevice::WriteOnly);
    in.setVersion(QDataStream::Qt_5_12);
    in<<MsgType::SET_MASTER;
    in<<bMaster;
    socket.write(data);
}

void tcpcClientForMVB::sendWriteMVBDataCommand(const mvbDeviceData& deviceData)
{
    QByteArray data;
    QDataStream in(&data, QIODevice::WriteOnly);
    in.setVersion(QDataStream::Qt_5_12);
    in<<MsgType::MVB_WRITE;
    in<<deviceData.m_portAddress;
    in<<deviceData.serialize();
    socket.write(data);
}

void tcpcClientForMVB::commandResetAllPorts()
{
    QByteArray data;
    QDataStream in(&data, QIODevice::WriteOnly);
    in.setVersion(QDataStream::Qt_5_12);
    in<<MsgType::RESET_ALL_PORTS;
    socket.write(data);
}
